/**
 *  Copyright 2010 ShenYang voole Co. Ltd.
 *  All right reserved. 
 */
package cn.piaoyi.dao.xml;

import cn.piaoyi.bean.Province;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.dao.ProvinceDAO;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
 * 功能描述:省操作实现
 *
 * 创建日期 Apr 10, 2010
 *
 * @author zhangbaochao
 *
 * @version 1.0
 *
 */
public class ProvinceXMLDAO implements ProvinceDAO {
	
	/**
	 * @param id
	 * @return Province
	 * 根据ID查找Province
	 */
	public Province findProvinceById(long id){
		try {
			File file = new File(Constant.ROOTPATH + Constant.ARTAXMLPATH);
			FileInputStream in = new FileInputStream(file);
			Reader reader = new InputStreamReader(in,"utf-8");
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(reader);
			List list = document.selectNodes("/Area/Province");
			Iterator iter = list.iterator();
			long provinceId = 0;
			while(iter.hasNext()){
				Element provinceElement = (Element)iter.next();
				Attribute idElement = (Attribute) provinceElement.attribute("ID");
				Attribute nameElement = (Attribute) provinceElement.attribute("Name");
				provinceId = Long.parseLong(idElement.getValue());
					if(provinceId == id){
					Province p = new Province();
					p.setId(Long.parseLong(idElement.getValue()));
					p.setName(nameElement.getValue());
					return p;
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @param name
	 * @return Province
	 * 根据name查找Province
	 */
	public Province findProvinceByName(String name){
		try {
			File file = new File(Constant.ROOTPATH + Constant.ARTAXMLPATH);
			FileInputStream in = new FileInputStream(file);
			Reader reader = new InputStreamReader(in,"utf-8");
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(reader);
			List list = document.selectNodes("/Area/Province");
			Iterator iter = list.iterator();
			long provinceId = 0;
			String provinceName = "";
			while(iter.hasNext()){
				Element provinceElement = (Element)iter.next();
				Attribute idElement = (Attribute) provinceElement.attribute("ID");
				Attribute nameElement = (Attribute) provinceElement.attribute("Name");
				provinceId = Long.parseLong(idElement.getValue());
				provinceName = nameElement.getValue();
				if(provinceName.equalsIgnoreCase(name)){
				Province p = new Province();
				p.setId(Long.parseLong(idElement.getValue()));
				p.setName(nameElement.getValue());
				return p;
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @return List
	 * 获取所有Province
	 */
	public List findProvinceAll(){
		List l = new ArrayList();
		try {		
			File file = new File(Constant.ROOTPATH + Constant.ARTAXMLPATH);
			FileInputStream in = new FileInputStream(file);
			Reader reader = new InputStreamReader(in,"utf-8");
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(reader);
			List list = document.selectNodes("/Area/Province");
			Iterator iter = list.iterator();
			while(iter.hasNext()){
				Element provinceElement = (Element)iter.next();
				Attribute idElement = (Attribute) provinceElement.attribute("ID");
				Attribute nameElement = (Attribute) provinceElement.attribute("Name");
				Province p = new Province();
				p.setId(Long.parseLong(idElement.getValue()));
				p.setName(nameElement.getValue());
				l.add(p);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return l;
	}
}
